package top.cyuw.subjectsystem.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.cyuw.subjectsystem.dto.PagingParamDto;
import top.cyuw.subjectsystem.dto.depart.DepartCreateRequest;
import top.cyuw.subjectsystem.dto.depart.DepartEditRequest;
import top.cyuw.subjectsystem.service.DepartService;
import top.cyuw.subjectsystem.vo.PagingVo;
import top.cyuw.subjectsystem.vo.ResponseVo;
import top.cyuw.subjectsystem.vo.depart.DepartVo;

import java.util.List;

@RestController
@RequestMapping("/api/depart")
public class DepartController {

    @Autowired
    DepartService departService;

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    ResponseVo<Boolean> create(@RequestBody DepartCreateRequest request) {
        return ResponseVo.success(departService.create(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    ResponseVo<Boolean> delete(@RequestParam Integer id) {
        return ResponseVo.success(departService.delete(id));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    ResponseVo<Boolean> edit(@RequestBody DepartEditRequest request) {
        return ResponseVo.success(departService.edit(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    ResponseVo<PagingVo<DepartVo>> search(@RequestBody PagingParamDto<String> request) {
        return ResponseVo.success(departService.search(request));
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    ResponseVo<List<DepartVo>> list() {
        return ResponseVo.success(departService.all());
    }

}
